/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ihmprojet.view.transformer;

import ihmprojet.core.model.Sommet;
import java.awt.Image;
import java.io.File;
import java.io.FileFilter;
import java.security.cert.Extension;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author nell
 */
public class IconLoader {

    private class ExtensionFileFilter implements FileFilter {

        private String extension;

        public ExtensionFileFilter(String extension) {
            this.extension = extension;
        }

        @Override
        public boolean accept(File pathname) {
            try {
                String name = pathname.getName();
                String fileExt = name.substring(name.lastIndexOf('.') + 1);
                return fileExt.equals(extension);
            } catch (Exception e) {
                return false;
            }
        }
    }
    private Map<Sommet.Type, Icon> icons = new HashMap();

    public void load(File dir, String extension) {
        if (!dir.isDirectory()) {
            return;
        }

        for (File f : dir.listFiles(new ExtensionFileFilter(extension))) {
            try {
                Sommet.Type type = Sommet.Type.valueOf(f.getName().substring(0, f.getName().lastIndexOf('.')).toUpperCase());
                Icon i = new ImageIcon(f.getAbsolutePath());
                icons.put(type, i);
            } catch (Exception ex) {
            }
        }
    }
    
    public Icon getIconFor(Sommet.Type type){
        return icons.get(type);
    }
}
